package com.guoranhao.admin.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;

import com.guoranhao.business.api.service.BaseInfoApi;
import com.guoranhao.business.enums.BusinessErrorCodeEnum;
import com.guoranhao.business.param.AdminGetAppFunctionIconListParam;
import com.guoranhao.business.param.AppFunctionIconParam;
import com.guoranhao.business.vo.AppFunctionIconAdminVo;
import com.guoranhao.tool.utils.JsonUtil;
import com.guoranhao.tool.utils.LogUtil;
import com.guoranhao.tool.vo.AdminResultVo;
import com.guoranhao.tool.vo.MessageVo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * Description:APP功能按钮Controller
 * Copyright (c) Department of Research and Development/Beijing
 * All Rights Reserved.
 * @version 1.0 2017年09月07日 下午5:44 by 宫珣（gongxun@cloud-young.cpm）创建
 */
@Controller
@RequestMapping(value="/baseinfo/functionicon/")
public class AppFunctionIconController {
    private static final Logger logger = LoggerFactory.getLogger(AppFunctionIconController.class);

    @Reference(version = "1.0.1",url = "dubbo://127.0.0.1:20881")
    private BaseInfoApi baseInfoApi;

    /**
     * Description:llb功能按钮查询
     * Version1.0 2017/9/7 下午5:48 by 宫珣（gongxun@cloud-young.com/）创建
     * @return  MessageVo
     */
    @ResponseBody
    @RequestMapping(value="getAppFunctionIconListForAdmin", method= RequestMethod.POST)
    public MessageVo<AdminResultVo<AppFunctionIconAdminVo>> getAppFunctionIconListForAdmin( AdminGetAppFunctionIconListParam param){
        try {
            MessageVo<AdminResultVo<AppFunctionIconAdminVo>> resultVo = baseInfoApi.getAppFunctionIconListForAdmin(param);
            LogUtil.info(logger, "AppFunctionIconController_llbAppFunctionIconList, resultVo:{0}", JSON.toJSONString(resultVo));
            return resultVo;
        } catch (Exception e) {
            LogUtil.error(logger, e, "AppFunctionIconController_llbAppFunctionIconList_invoke_error", JsonUtil.Object2Json(param));
        }
        return new MessageVo<>(false, BusinessErrorCodeEnum.BASE_INFO_ERR_CODE_1204000.getCode(), BusinessErrorCodeEnum.BASE_INFO_ERR_CODE_1204000.getMsg());
    }

    /**
     * Description:修改llb功能按钮图片（已合并到modifyAppFunctionIcon）
     * Version1.0 2017/9/7 下午6:29 by 宫珣（gongxun@cloud-young.com/）创建
     * @param appFunctionIconId     主键
     * @param functionIconUrl       图片
     * @return                      ture／false
     */
    @Deprecated
    @ResponseBody
    @RequestMapping(value="modifyAppFunctionIconListForAdmin", method= {RequestMethod.GET, RequestMethod.POST})
    public MessageVo modifyAppFunctionIconListForAdmin(@RequestParam Long appFunctionIconId, @RequestParam String functionIconUrl){
        if (null == appFunctionIconId) {
            return new MessageVo<>(false, BusinessErrorCodeEnum.BASE_INFO_ERR_CODE_1204005.getCode(), BusinessErrorCodeEnum.BASE_INFO_ERR_CODE_1204005.getMsg());
        }
        if (StringUtils.isBlank(functionIconUrl)) {
            return new MessageVo<>(false, BusinessErrorCodeEnum.BASE_INFO_ERR_CODE_1204006.getCode(), BusinessErrorCodeEnum.BASE_INFO_ERR_CODE_1204006.getMsg());
        }

        try {
            MessageVo<Boolean> resultVo = baseInfoApi.modifyAppFunctionIconListForAdmin(null, appFunctionIconId, functionIconUrl);
            LogUtil.info(logger, "AppFunctionIconController_modifyAppFunctionIconListForAdmin, resultVo:{0}", resultVo);
            return new MessageVo<>(true, resultVo);
        } catch (Exception e) {
            LogUtil.error(logger, e, "AppFunctionIconController_modifyAppFunctionIconListForAdmin_invoke_error");
        }
        return new MessageVo<>(false, BusinessErrorCodeEnum.BASE_INFO_ERR_CODE_1204001.getCode(), BusinessErrorCodeEnum.BASE_INFO_ERR_CODE_1204001.getMsg());
    }

    /**
     * Description: 修改llb功能图标信息
     * Version1.0 2019/6/3 11:52 by 赵博（zhaobo@cloud-young.com）创建
     * @param param 参数
     * @return MessageVo
     */
    @ResponseBody
    @RequestMapping(value="modifyAppFunctionIcon", method= {RequestMethod.POST, RequestMethod.GET})
    public MessageVo modifyAppFunctionIcon(@RequestBody AppFunctionIconParam param){
        if ( Objects.isNull(param)) {
            return new MessageVo<>(false, BusinessErrorCodeEnum.ACTIVITY_ERR_10000.getCode(), BusinessErrorCodeEnum.ACTIVITY_ERR_10000.getMsg());
        }
        if (Objects.isNull(param.getAppFunctionIconId())) {
            return new MessageVo<>(false, BusinessErrorCodeEnum.BASE_INFO_ERR_CODE_1204005.getCode(), BusinessErrorCodeEnum.BASE_INFO_ERR_CODE_1204005.getMsg());
        }

        try {
            return baseInfoApi.modifyAppFunctionIcon(param);
        } catch (Exception e) {
            LogUtil.error(logger, e, "AppFunctionIconController_modifyAppFunctionIcon_invoke_error,param:{0}", JsonUtil.Object2Json(param));
        }
        return new MessageVo<>(false, BusinessErrorCodeEnum.BASE_INFO_ERR_CODE_1204001.getCode(), BusinessErrorCodeEnum.BASE_INFO_ERR_CODE_1204001.getMsg());
    }

    /**
     * Description: 功能图标上下线
     * Version1.0 2019/6/3 11:52 by 赵博（zhaobo@cloud-young.com）创建
     * @param param 参数
     * @return MessageVo
     */
    @ResponseBody
    @RequestMapping(value="updateIconStatus", method= {RequestMethod.GET, RequestMethod.POST})
    public MessageVo updateIconStatus(AppFunctionIconParam param){
        if ( Objects.isNull(param)) {
            return new MessageVo<>(false, BusinessErrorCodeEnum.ACTIVITY_ERR_10000.getCode(), BusinessErrorCodeEnum.ACTIVITY_ERR_10000.getMsg());
        }
        if (Objects.isNull(param.getAppFunctionIconId()) || Objects.isNull(param.getStatus())) {
            return new MessageVo<>(false, BusinessErrorCodeEnum.BASE_INFO_ERR_CODE_1204005.getCode(), BusinessErrorCodeEnum.BASE_INFO_ERR_CODE_1204005.getMsg());
        }
        try {
            return baseInfoApi.modifyAppFunctionIcon(param);
        } catch (Exception e) {
            LogUtil.error(logger, e, "AppFunctionIconController_updateIconStatus_invoke_error,param:{0}", JsonUtil.Object2Json(param));
        }
        return new MessageVo<>(false, BusinessErrorCodeEnum.BASE_INFO_ERR_CODE_1204001.getCode(), BusinessErrorCodeEnum.BASE_INFO_ERR_CODE_1204001.getMsg());
    }



    /**
     * Description: 新增icon信息
     * Version1.0 2019/6/3 16:05 by 赵博（zhaobo@cloud-young.com）创建
     * @param param 参数
     * @return MessageVo
     */
    @ResponseBody
    @RequestMapping(value="addNewFunctionIcon", method= {RequestMethod.POST, RequestMethod.GET})
    public MessageVo addNewFunctionIcon(@RequestBody AppFunctionIconParam param){
        if ( Objects.isNull(param)) {
            return new MessageVo<>(false, BusinessErrorCodeEnum.ACTIVITY_ERR_10000.getCode(), BusinessErrorCodeEnum.ACTIVITY_ERR_10000.getMsg());
        }
        if (Objects.isNull(param.getAppFunctionIconCategoryId())) {
            return new MessageVo<>(false, BusinessErrorCodeEnum.BASE_INFO_ERR_CODE_1204007.getCode(), BusinessErrorCodeEnum.BASE_INFO_ERR_CODE_1204007.getMsg());
        }

        try {
            return baseInfoApi.addNewFunctionIcon(param);
        } catch (Exception e) {
            LogUtil.error(logger, e, "AppFunctionIconController_addNewFunctionIcon_invoke_error,param:{0}", JsonUtil.Object2Json(param));
        }
        return new MessageVo<>(false, BusinessErrorCodeEnum.BASE_INFO_ERR_CODE_1204002.getCode(), BusinessErrorCodeEnum.BASE_INFO_ERR_CODE_1204002.getMsg());
    }


}
